<template>
    <div class="fix-page">
<div id="login" class="place">
        <headerNav :isShowBack="true" title="登录"></headerNav>
        <div class="a"><a href=""></a></div>
        <div class="title">
            <h2>使用账户密码登录</h2>
            <p class="p1">使用已注册手机号或邮箱登录</p >
        </div>
        <div class="phone-num">
            <p class="phone">手机号码/电子邮箱地址</p><br>
            <input class="num" type="text"   v-model="user.username" ><br>
            <p style="margin: 10px 0;color: black">密码</p><br>
            <input class="num" type="password" v-model="user.password"><br>
            <input @click="confirmLogin()" class="yzm" type="button" value="登录" ><br>
            <a class="left" @click="$router.push('/security_code')">手机动态密码登录</a>
            <a class="right" @click="$router.push('/forget_pwd')">忘记密码？</a>
            <div class="text2">
            <p>使用其他方式登录</p>
            <img src="../../assets/img/login_img/ddd.png">
            </div>
        </div>
        <div class="foot">
            <a>注册/登录即代表您年满18岁，已认真阅读并同意接受Rook《服务条款》、《隐私政策》，并同意订阅Rook特惠信息和政策更新，您可在设置中随时退订。</a>
        </div>
    </div>
    </div>
</template>

<script>
    import headerNav from 'components/HeaderNav/headerNav'
    import {loginUser} from 'network/index'
    export default {
        name: "phone_login",
        components:{
            headerNav
        },
        data(){
            return{
               user:{
                   username:"",
                   password:""
               }
            }
        },
        mounted(){
        },
        methods:{
            confirmLogin(){
                const phoneReg = /^1[3456789]\d{9}$/;
                const emailReg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
                if(!this.user.username){
                    this.$toast({
                        message: '请填写手机号或者邮箱',
                        type:"fail"
                    });
                    return
                }else if(!this.user.password){
                    this.$toast({
                        message: '请输入密码',
                        type:"fail"
                    });
                    return
                } else if(emailReg.test(this.user.username)){
                    console.log(this.user);
                    loginUser(this.user).then(res=>{
                        if(res.data){
                            this.$store.dispatch("setInfo",res.data)
                        }
                        this.$router.push("/mine")
                        this.$toast({
                            message: '登录成功',
                        });
                    }).catch(err=>{
                        this.$toast({
                            message: '登录失败，服务端错误',
                            type:"fail"
                        });
                    })
                    return
                }else if(phoneReg.test(this.user.username)){
                    console.log(this.user);
                    loginUser(this.user).then(res=>{
                        if(res.data){
                            this.$store.dispatch("setInfo",res.data)
                        }
                        this.$router.push("/mine")
                        this.$toast({
                            message: '登录成功',
                        });
                    }).catch(err=>{
                        this.$toast({
                            message: '登录失败，服务端错误',
                            type:"fail"
                        });
                    })
                  return
                }
                this.$toast({
                    message: '请输入正确的手机或者邮箱信息',
                    type:"fail"
                });
            }
        }
    }
</script>

<style scoped>
 .fix-page{
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        background: #fff;
        z-index: 777;
    }
    .fix-page .place{
        overflow: auto;
        height: 100%;
    }
.text2{
      text-align: center;
      margin-top: 80px;
    }
   #login{
      padding: 0 20px 80px;
   }
  .num{
      border: none;
     border-bottom: 1px solid #ccc;
      height: 30px;
      width: 100%;
  }
  .yzm{
      height: 50px;
      width: 100%;
      text-align: center;
      background-color: rgb(0, 138, 133);
      border-radius:8px;
      color:cornsilk;
      border: none;
      margin: 20px 0;
  }
  .foot{
      font-size: 13px;
      margin-right: 20px;
      text-align: center;
      margin-top: 30px;
      bottom: 55px;
      line-height: 1.4;
  }
  .phone{
      margin-top: 30px;
      color: black;
  }
  .left{
      margin-top: 16px;
      color: rgb(0, 138, 133);
  }
  .right{
      margin-top: 16px;
      color: rgb(127, 127, 127);
  }
  .p1{
      margin-top: 10px;
  }
  .title{
      padding-top:10px;
  }
</style>